<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" @click="addFlow">增加</el-button>
    <el-table align="center" :data="flowLists" style="width 100%">
      <el-table-column prop="number" label="流程序号" width="180"></el-table-column>
      <el-table-column prop="creator" label="申请人" :formatter="adminListFormatCreator" width="180"></el-table-column>
      <el-table-column prop="agree_user" label="审核人" :formatter="adminListFormatAgreeUser" width="180"></el-table-column>
      <el-table-column prop="manager" label="批准人（备用）" :formatter="adminListFormatManager" width="180"></el-table-column>
      <el-table-column label="操作" fixed="right" width="100">
        <template slot-scope="scope">
          <el-button @click="editClick(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="新增流程" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" label-width="80px">
        <el-form-item label="申请人">
          <el-select v-model="addForm.creator" placeholder="请选择用户">
              <template v-for="item in adminList">
                <el-option :label="item.name" :value="item.username" :key="item.username"></el-option>
              </template>
            </el-select>
        </el-form-item>
        <el-form-item label="审核人">
          <el-select v-model="addForm.agree_user" placeholder="请选择用户">
              <template v-for="item in adminList">
                <el-option :label="item.name" :value="item.username" :key="item.username"></el-option>
              </template>
            </el-select>
        </el-form-item>
        <el-form-item label="批准人">
          <el-select v-model="addForm.manager" placeholder="请选择用户">
              <template v-for="item in adminList">
                <el-option :label="item.name" :value="item.username" :key="item.username"></el-option>
              </template>
            </el-select>
        </el-form-item>
        <el-button type="primary" @click="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addSubmit();addFormVisible = false">增加</el-button>
      </el-form>
    </el-dialog>
    <el-dialog title="编辑流程" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form ref="editForm" :model="editForm" label-width="80px">
        <el-form-item label="申请人">
          <el-select v-model="editForm.creator" placeholder="请选择用户">
              <template v-for="item in adminList">
                <el-option :label="item.name" :value="item.username" :key="item.username"></el-option>
              </template>
            </el-select>
        </el-form-item>
        <el-form-item label="审核人">
          <el-select v-model="editForm.agree_user" placeholder="请选择用户">
              <template v-for="item in adminList">
                <el-option :label="item.name" :value="item.username" :key="item.username"></el-option>
              </template>
            </el-select>
        </el-form-item>
        <el-form-item label="批准人">
          <el-select v-model="editForm.manager" placeholder="请选择用户">
              <template v-for="item in adminList">
                <el-option :label="item.name" :value="item.username" :key="item.username"></el-option>
              </template>
            </el-select>
        </el-form-item>
        <el-button type="primary" @click="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click="editSubmit();editFormVisible = false">编辑</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      flowLists: [],
      operatetype: "",
      number: "",
      creator: "",
      agreeuser: "",
      manager: "",
      addFormVisible: false,
      editFormVisible: false,
      addForm: {
        creator: "",
        agree_user: "",
        manager: ""
      },
      editForm: {
        number: "",
        creator: "",
        agree_user: "",
        manager: ""
      },
      adminList: []
    };
  },
  created() {
    this.fetchAdminList();
    this.fetchdata();
  },
  methods: {
    fetchAdminList() {
      this.axios
        .get("/get_adminlist")
        .then(response => {
          this.adminList = response.data;
        })
        .catch(error => {
          console.log(error);
        });
    },
    fetchdata() {
      this.axios
        .get("/flowmanage")
        .then(response => {
          this.flowLists = response.data;
        })
        .catch(error => {
          console.log(error);
        });
    },
    addSubmit() {
      this.axios
        .get("/flowmanage", {
          params: {
            operatetype: 1,
            creator: this.addForm.creator,
            agreeuser: this.addForm.agree_user,
            manager: this.addForm.manager
          }
        })
        .then(response => {
          this.flowLists = response.data;
          this.$message({
            message: "添加成功",
            type: "success"
          });
        })
        .catch(error => {
          console.log(error);
        });
    },
    editSubmit() {
      this.axios
        .get("/flowmanage", {
          params: {
            operatetype: 2,
            number: this.editForm.number,
            creator: this.editForm.creator,
            agreeuser: this.editForm.agree_user,
            manager: this.editForm.manager
          }
        })
        .then(response => {
          this.flowLists = response.data;
          this.$message({
            message: "编辑成功",
            type: "success"
          });
        })
        .catch(error => {
          console.log(error);
        });
    },
    addFlow() {
      this.addFormVisible = true;
    },
    editClick(row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    adminListFormatCreator(row) {
      return this.adminList.find(element => element.username == row.creator)
        .name;
    },
    adminListFormatAgreeUser(row) {
      return this.adminList.find(element => element.username == row.agree_user)
        .name;
    },
    adminListFormatManager(row) {
      return this.adminList.find(element => element.username == row.manager)
        .name;
    }
  }
};
</script>
